package com.somsoft.handjc.activity.tongji.xzsp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.SuperChart;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.apache.http.NameValuePair;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.somsoft.handjc.activity.MainActivity;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;
import com.somsoft.handjc.util.XzspDataUtil;
import com.somsoft.handjc.xzzf.po.MonthInfo;

/**
 * 受理业务时间分布统计分析
 *
 */
public class BusinessTimeLineChart extends SuperChart{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		List<MonthInfo> monthInfos = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
        	monthInfos = XzspDataUtil.businessTime(params);
		} catch (Exception e) {
			Log.e("BusinessTimeLineChart", e.getMessage(),e);
			Toast.makeText(BusinessTimeLineChart.this, "加载数据失败！", Toast.LENGTH_SHORT).show();
		}
		try{
			String[] titles = new String[] { "受理业务量" };
			

			List<Date[]> x = new ArrayList<Date[]>();
			for (int i = 0; i < titles.length; i++) {
				Date[] dates = new Date[12];
				for (int j = 0; j < 12; j++) {
					dates[j] = sdf.parse(monthInfos.get(j).getMonthName());
				}
				x.add(dates);
			}
			List<double[]> values = new ArrayList<double[]>();

			double[] normal = new double[12];
			double tmpMax = 0;
			for (int i = 0; i < 12; i++) {
				normal[i] = monthInfos.get(i).getMonthCount();
				if(normal[i]>tmpMax){
					tmpMax = normal[i];
				}
			}
			values.add(normal);
			int[] colors = new int[] { Color.rgb(77, 198, 244) };
			PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };
			XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
			int length = renderer.getSeriesRendererCount();
			for (int i = 0; i < length; i++) {
				((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
						.setFillPoints(true);
			}
			setChartSettings(renderer, "受理业务时间分布统计分析", "",
					"", x.get(0)[0].getTime(),
					x.get(0)[11].getTime(), -5, tmpMax*1.1, Color.LTGRAY, Color.LTGRAY);
			renderer.setXLabels(10);
			
			renderer.setYLabels(10);
			renderer.setXLabelsAlign(Align.CENTER);
			renderer.setYLabelsAlign(Align.RIGHT);
			
			renderer.setMargins(new int[]{20,60,70,20});//上/左/下/右
			
			for (int i = 0; i < length; i++) {
		      SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
		      seriesRenderer.setChartValuesTextSize(30);
		      seriesRenderer.setChartValuesTextAlign(Align.RIGHT);
		      seriesRenderer.setDisplayChartValues(true);
		      seriesRenderer.setChartValuesSpacing(15);
		    }
		    
		    View bar = ChartFactory.getTimeChartView(this,buildDateDataset(titles, x, values), renderer, "yyyy-MM");
		    setContentView(bar);
		    Utils.closeLoading();
		} catch (Exception e) {
			Log.e("BusinessTimeLineChart", e.getMessage(),e);
		}
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {			
		menu.add(Menu.NONE, 10, 10, "首页").setIcon(android.R.drawable.ic_menu_compass);
		menu.add(Menu.NONE, 15, 15, "列表").setIcon(android.R.drawable.ic_menu_info_details);
		menu.add(Menu.NONE, 90, 90, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		return true;											
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		// 选中某个菜单项
		Intent it = null;
		switch (item.getItemId()) {								// 判断菜单项ID
		case  10:
			it = new Intent(BusinessTimeLineChart.this,MainActivity.class);
			startActivity(it);
			break;
		case  15:
			it = new Intent(BusinessTimeLineChart.this,XzspTongJiMainActivity.class);
			startActivity(it);
			break;
		case  90:
			MyApplication.getInstance().exit();
			break;
		}
		return false;
	}

}
